package com.youkeda.app.control;

import com.youkeda.app.model.Result;
import com.youkeda.app.service.TicketPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName SnappedUpController
 * @Author 刘正星
 * @Date 2020/8/25 8:08
 **/
@Controller
public class SnappedUpController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TicketPurchaseService ticketPurchaseService;
    @GetMapping("/ticket")
    @ResponseBody
    public Result<Boolean> snapperUp(long id){
        //初始化抢票的数据
        for (int i = 0; i < 13; i++) {
            new Thread(new Runnable() {
                public void run() {
                    ticketPurchaseService.ticketPurchase(id);
                }
            }).start();

        }
        return new Result<>();
    }

    @GetMapping("/getkey")
    @ResponseBody
    public Result getkey()  {
            redisTemplate.opsForValue().set("book","java",1000, TimeUnit.MILLISECONDS);
        System.out.println(redisTemplate.opsForValue().get("book"));
        Result result = new Result();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        result.setData(redisTemplate.opsForValue().get("book"));
        return result;
    }

}
